<template>
  <div id="bg">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色配置</el-breadcrumb-item>
    </el-breadcrumb>
    <div id="tab">
      <!-- 搜索栏 -->
      <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="formInline.user" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="启用" value="启用"></el-option>
              <el-option label="禁用" value="禁用"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form>
      </div>
      <el-divider></el-divider>
      <!-- 新增按钮 -->
      <el-button type="primary" @click="addVisible = true">新增</el-button>
      <!-- 表格数据 -->
      <div id="table">
        <el-table :data="tablesData" stripe style="width: 100%">
          <el-table-column prop="r_id" label="角色ID" width="180">
          </el-table-column>
          <el-table-column prop="r_name" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="department_name" label="部门">
          </el-table-column>
          <el-table-column prop="r_state" label="状态">
          </el-table-column>
          <el-table-column prop="r_operator" label="操作人">
          </el-table-column>
          <el-table-column prop="r_date" label="操作时间">
          </el-table-column>
          <el-table-column prop="operate" label="操作">
            <template slot-scope="scope">
              <el-button type="warning" @click="handleClick(scope.row)" size="small">编辑</el-button>
              <el-button type="danger" @click="handleDel(scope.row)" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div id="pages">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!-- 新增弹框 formDialog-->
    <el-dialog title="新增角色" :visible.sync="addVisible" width="40%">
      <el-form :model="formDialog">
        <el-form-item label="角色ID" :label-width="formLabelWidth">
          <el-input v-model="formDialog.num" autocomplete="off" width="10px" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="formDialog.name" autocomplete="off" placeholder="例如：财务"></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input v-model="formDialog.department" autocomplete="off" placeholder="例如：1"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="formDialog.state" placeholder="请选择状态">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作人" :label-width="formLabelWidth">
          <!-- <el-input v-model="formDialog.operator" autocomplete="off"></el-input> -->
          <el-select v-model="formDialog.operator" placeholder="请选择状态">
            <el-option label="系统管理员" value="系统管理员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" v-model="formDialog.date" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOk">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑弹框 formsDialog-->
    <el-dialog title="角色配置编辑" :visible.sync="editVisible" width="40%" :before-close="handleClose">
      <el-form :model="formsDialog">
        <el-form-item label="角色ID" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.nums" autocomplete="off" width="10px" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.names" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.departments" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="formsDialog.states" placeholder="请选择状态">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作人" :label-width="formLabelWidth" disabled>
          <el-input v-model="formsDialog.operators" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="操作时间" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" v-model="formsDialog.dates" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOk">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除功能 handleDel -->
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      addVisible: false,
      editVisible: false,
      formInline: {
        user: '',
        state: ''
      },
      formDialog: {
        num: '',
        name: '',
        department: '',
        state: '',
        operator: '',
        date: ''
      },
      formsDialog: {
        nums: '',
        names: '',
        departments: '',
        states: '',
        operators: '',
        dates: ''
      },
      tableData: [],  //后台请求的全部数据
      page: 1,  //当前处于多少页
      pageSize: 5,  //一页可以选择多少条数据
      total: 0,  //一共有多少条数据
      formLabelWidth: '120px'
    }
  },
  methods: {
    onSearch() {
      // console.log(this.formInline.user)
      // console.log(this.formInline.state);
      this.page = 1;
      // if (this.formInline.user == '' && this.formInline.state == '') {
      //   this.getData();
      // } else {
        this.$axios({
          url: '/system/systemrole/get',
          params: {
            name: this.formInline.user,
            state: this.formInline.state,
          },
        }).then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
          // console.log(res.data.data);
        })
      // }
    },
    onReset() {
      this.page = 1;
      this.getData();
      this.formInline.user = '';
      this.formInline.state = '';
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.onSearch();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleClick(item) {
      // console.log(item);
      this.editVisible = true;
      this.formsDialog = {
        nums: item.r_id,
        names: item.r_name,
        departments: item.department_name,
        states: item.r_state,
        operators: item.r_operator,
        dates: item.r_date,
      }
    },
    handleDel(data) {
      console.log(data);
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          url: '/system/systemrole/del',
          method: 'post',
          data: {
            r_id: data.r_id
          }
        }).then(() => {
          this.getData();
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addOk() {
      console.log(this.formDialog)
      this.addVisible = false;
      this.$axios({
        url: '/system/systemrole/add',
        method: 'post',
        data: {
          // r_id: this.formDialog.num,
          r_name: this.formDialog.name,
          r_depart: this.formDialog.department,
          r_state: this.formDialog.state,
          r_operator: this.formDialog.operator,
          r_date: this.formDialog.date,
        }
      }).then((res) => {
        this.getData();
      })
      this.formDialog = {
        num: '',
        name: '',
        department: '',
        state: '',
        operator: '',
        date: ''
      }
    },
    editOk() {
      this.editVisible = false;
      this.$axios({
        url: '/system/systemrole/edit',
        method: 'post',
        data: {
          r_id: this.formsDialog.nums,
          r_name: this.formsDialog.names,
          r_department: this.formsDialog.departments,
          r_state: this.formsDialog.states,
          r_operator: this.formsDialog.operators,
          r_date: this.formsDialog.dates,
        }
      }).then((res) => {
        this.getData();
      })
    },
    // 渲染表格的数据
    getData() {
      this.$axios({
        url: '/system/systemrole/get',
        params: {
          name: '',
          state: '',
        },
      }).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total;
      }).catch(error => {
        console.log(error);
      })
    }
  },
  created: function () {
    this.getData();
  },
  computed: {
    // 前端分页，把要用的数据放到tablesData里面
    tablesData() {
      return this.tableData.slice((this.page - 1) * this.pageSize, this.page * this.pageSize)
    }
  }
}
</script>
<style scoped>
.el-row span {
  cursor: pointer;
}
.el-main,
#bg {
  height: 100%;
}
.el-breadcrumb {
  margin: 20px;
}
#closeBtn {
  text-align: right;
}
#bg {
  background-color: rgb(242, 242, 242);
}
#tab {
  padding: 30px;
  background-color: #fff;
  margin: 0 20px;
}
.el-breadcrumb {
  margin-top: 0;
  padding-top: 20px;
}
#pages {
  margin-top: 20px;
  text-align: right;
}
.el-select {
  width: 100%;
}
.el-form-item {
  padding-right: 40px;
}
</style>
